package com.shine.app.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.shine.app.R;
import com.shine.app.bean.AddressBean;
import com.shine.app.bean.CourseImageBean;
import com.shine.app.bean.MyFavoriteBean;
import com.shine.app.ui.ViewMyInfo_FavoriteDeleteDialogActivity;

public class MyFavoriteAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;
	private Context context;
	private List<MyFavoriteBean> data;
	private Handler handler;

	public MyFavoriteAdapter() {
	}

	public MyFavoriteAdapter(Context context, List<MyFavoriteBean> list,
			ImageLoader loader, Handler handlers) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.data = list;
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.noimages)
				.showImageOnFail(R.drawable.noimages).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		this.imageLoader = loader;
		this.handler = handlers;
	}

	public List<MyFavoriteBean> getData() {
		return data;
	}

	public void setData(List<MyFavoriteBean> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.viewmyinfo_favorite_item,
					null);
			holder = new ViewHolder();
			holder.progressBar = (ProgressBar) convertView
					.findViewById(R.id.progressBar);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.imageView);
			holder.ratingBar = (RatingBar) convertView
					.findViewById(R.id.ratingbar);
			holder.nameTextView = (TextView) convertView
					.findViewById(R.id.name);
			holder.addressTextView = (TextView) convertView
					.findViewById(R.id.address);
			holder.zipTextView = (TextView) convertView.findViewById(R.id.zip);
			holder.urlTextView = (TextView) convertView.findViewById(R.id.url);
			holder.idTextView = (TextView) convertView.findViewById(R.id.id);
			holder.removeButton = (Button) convertView
					.findViewById(R.id.remove);
			holder.findteetimeButton = (Button) convertView
					.findViewById(R.id.findteetime);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final MyFavoriteBean favoriteBean = data.get(position);
		holder.nameTextView.setText(favoriteBean.getName());
		holder.addressTextView.setText(favoriteBean.getAddress().getAddress1());
		holder.zipTextView.setText(AddressBean.getCityAddress(favoriteBean
				.getAddress()));
		holder.urlTextView.setText("");
		holder.ratingBar.setRating((float) favoriteBean.getReviewAverage());
		holder.idTextView.setTag(favoriteBean);
		CourseImageBean[] images = favoriteBean.getImages();
		String imageUrl = "";
		if (images.length > 0) {
			imageUrl = images[0].getUrl();
		}
		imageLoader.displayImage(imageUrl, holder.imageView, options,
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingStarted(String imageUri, View view) {
						holder.progressBar.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						holder.progressBar.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						holder.progressBar.setVisibility(View.GONE);
					}
				}, null);
		holder.removeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context,
						ViewMyInfo_FavoriteDeleteDialogActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				intent.putExtra("myFavoriteBean", favoriteBean);
				context.startActivity(intent);
			}
		});

		holder.findteetimeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Message message = new Message();
				message.what = 1;
				handler.sendMessage(message);
			}
		});
		return convertView;
	}

	static class ViewHolder {
		private ProgressBar progressBar;
		private ImageView imageView;
		private RatingBar ratingBar;

		private TextView nameTextView;
		private TextView addressTextView;
		private TextView zipTextView;
		private TextView urlTextView;
		private TextView idTextView;

		private Button removeButton;
		private Button findteetimeButton;
	}
}
